<?php
    session_start();
    //these will need to be replaced
    //$ticketID = 1;
   // $_SESSION['userId'] = 6;
?>

<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <LINK REL=StyleSheet HREF="maintenancePagesCSS.css" TYPE="text/css" />
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
        <?php
        include "Datasource.php";
        include "WorkDone.php";
        include "WorkDoneDao.php";
        include "EmployeeDAO.php";
        include "Employee.php";
         $objDatasource = new Datasource("localhost", "theanykey", "root", "");   
         $objEmpDAO = new EmployeeDao();
         $loggedInEmployee = $objEmpDAO->getObject($objDatasource, $_SESSION['userId']);
         $objUserName = $loggedInEmployee->getLoginID();
         $role = $loggedInEmployee->getRole();
         print("Current User: $objUserName<br/>Role: $role");
        ?>
        <div id="divWorkDone">
         <?php
         $objWorkDoneDAO = new WorkDoneDao();
         $count = 1;
         while($_POST['wdPK' . "$count"])
         {
           $objWorkDone = new WorkDone();
           $objWorkDone->setTechAssigned($_POST['wdUser' . "$count"]);
           $objWorkDone->setDateDone( $_POST['wdDate' . "$count"]);
           $objWorkDone->setHrsWorkDone( $_POST['wdHours' . "$count"]);
           $objWorkDone->setNotes($_POST['wdNotes' . "$count"]);
           $objWorkDone->setTicketID($ticketID);//need to change this to the the ticket ID that is produced
           $count++;
           $objWorkDoneDAO->create($objDatasource, $objWorkDone);
         }
        ?>
        <script type="text/javascript">
            function removeLast()
            {
                table = document.getElementById("tblWorkDone");
                table.deleteRow(table.rows.length - 1);
            }
            function editRow(value)
            {
                alert(value);
            }
        </script>
 <center>
<h2> Work Done</h2>
<br/>
<form action="workDoneDiv.php" method="post">
       <table class="EditTable">
           <tr><td colspan="2" align="center"><b>Work Done Functions</b></td></tr>
           <tr>
               <td>
                   <input type="button" value="Add Work Done" onclick="workDonePopup();" />
               </td>
               <td>
                   <input type="button" value="Remove Last Addition" onclick="removeLast();" />
               </td>
           </tr>
       </table>
        <br/>
        <br/>
        <div id="workDoneOld">
            <div style="width:95%; border:solid; border-width:1; background-color: #666666;">
            <table style="width:96%" id="tblWorkDoneHeader">
                        <tr >
                            <th align="center" style="width:20%">
                                Tech
                            </th>
                            <th align="center" style="width:20%">
                                Date
                            </th>
                            <th align="center" style="width:40%">
                                Notes&nbsp;&nbsp;
                            </th>
                            <th align="center" style="width:20%">
                                Hours&nbsp;&nbsp;&nbsp;
                            </th>
                        </tr>
             </table>
            </div>
        <div id="divTblWorkDoneAdd" style="border:solid; border-width:1; width:95%; height:30%; overflow:scroll; ">
                <center>
                    <table cellpadding="0" cellspacing="0" style="width:96%;" id="tblWorkDone">
                        <tr>
                            <td align="center" style='width: 20%'>

                            </td>
                            <td align="center" style='width: 20%'>

                            </td>
                            <td align="center" style='width: 40%'>

                            </td>
                            <td align="right" style='width: 20%'>

                            </td>
                            <td>
                                
                            </td>
                        </tr>
                    </table>
                </center>
        </div>
        <br/>
           <input type="Submit" id="submitWorkDone" value="Submit" />
            </div>
        <br/>
        <br/>
         <?php
                            //populate table rows here
                            $objCurrentRows = new WorkDone();
                            $objCurrentRows->setTicketID($ticketID);
                            $rows = $objWorkDoneDAO->searchMatching($objDatasource, $objCurrentRows);
                            if($rows)
                            {
                                ?>
        <h3>Work Already Associated with Ticket #: <?php echo $ticketID; ?></h3>
        <br/>
            <div style="width:95%; border:solid; border-width:1; background-color: #666666;">
            <table style=" width:96%" id="tblWorkDoneOldHeader">
                        <tr >
                            <th align="center" style="width:20%">
                                Action
                            </th>
                            <th align="center" style="width:20%">
                                Tech
                            </th>
                            <th align="center" style="width:20%">
                                Date
                            </th>
                            <th align="center" style="width:20%">
                                Notes
                            </th>
                            <th align="center" style="width:20%">
                                Hours&nbsp;
                            </th>
                        </tr>
             </table>
            </div>
        <div style="border:solid; border-width:1; width:95%; height:30%; overflow:scroll; ">
                <center
                    <table style="width:96%;" id="tblWorkDoneOld" cellpadding="0" cellspacing="0">
                        <tr><td align="center" style='width: 20%'> </td><td align="center" style='width: 20%'></td><td align="center" style='width: 20%'> </td><td align="right" style='width: 20%'></td><td></td> </tr>
                               
                               <?php
                               $ndx = 1;
                               foreach($rows as $objWD)
                                {
                                        $rowColor = ($ndx % 2) == 0 ? "#D8D8D8" : "";
                                        $tech = $objWD->getTechAssigned();
                                        $empDAO = new EmployeeDao();
                                        $objEmployee = new Employee();
                                        $objEmployee = $empDAO->getObject($objDatasource, $tech);
                                        $techName = $objEmployee->getLastName() . ", " . $objEmployee->getFirstName();
                                        $hyperLink = false;
                                        if($objEmployee->getLoginID() == $loggedInEmployee->getLoginID()
                                            ||  $loggedInEmployee->getRole() == "Admin")
                                        {
                                            $hyperLink = true;
                                        }
                                        $date = $objWD->getDateDone();
                                        $notes = $objWD->getNotes();
                                        $hours = $objWD->getHrsWorkDone();
                                        $pk = $objWD->getWrkDoneID();
                                        //back to this
                                        if(strlen($notes) > 20)
                                        {
                                            $displayNotes = substr($notes, 0, 20) . "...";
                                        }
                                        else
                                        {
                                            $displayNotes = $notes;
                                        }
                                        print(" <tr id='rowWorkDoneOld$ndx' style='background-color: $rowColor'>
                                                    <td align='center'>");
                                        if($hyperLink)
                                        {
                                            print("<input type='button' value='Edit' onclick=\"editWorkDonePopup($pk, document.getElementById('rowWorkDoneOld$ndx').rowIndex, 'Edit');\" />
                                                   <input type='button' value='Delete'  onclick=\"editWorkDonePopup($pk, document.getElementById('rowWorkDoneOld$ndx').rowIndex, 'Delete');\"/>");
                                        }
                                        else
                                        {
                                            print("<input type='button' value='View' onclick=\"editWorkDonePopup($pk, document.getElementById('rowWorkDoneOld$ndx').rowIndex, 'View');\" />");
                                        }
                                        print("    </td>
                                                    <td align='center'>
                                                        $techName
                                                    </td>
                                                    <td align='center'>
                                                        $date
                                                    </td>
                                                    <td align='center'>
                                                      <span title='$notes'>$displayNotes</span>
                                                    </td>
                                                    <td align='center'>
                                                        $hours
                                                    </td>
                                                </tr>");
                                   $ndx++;
                               }//end foreach
                            }//end if
                        ?>
                    </table>
                </center>
        </div>
     <br/>
     <br/>

</form>
     <script type="text/javascript">
         function checkHiddenValue()
         {
            // alert("called");
             alert(document.getElementById("wdPK1").value);
         }
       function getBrowser()
       {
            return navigator.appName;
       }
       function workDonePopup()
       {
           if(getBrowser() == 'Microsoft Internet Explorer')
           {
               //showModalDialog does not yield a window.opener type in IE
               //stupid IE!
               window.open('WorkDonePopup.php', null, 'width=500, height=300');
           }
           else
           {
                window.showModalDialog('WorkDonePopup.php', null, 'dialogHeight:300px;dialogWidth:500px;dialogLeft:300;dialogTop:200');
           }
       }
       function editWorkDonePopup(workDoneID, rowID, editOrDelete)
       {
           if(getBrowser() == 'Microsoft Internet Explorer')
           {
               //showModalDialog does not yield a window.opener type in IE
               //stupid IE!
               window.open("EditWorkDonePopup.php?id=" + workDoneID + "&row=" + rowID + "&function=" + editOrDelete, null, 'width=500, height=300');

           }
           else
           {
                window.showModalDialog("EditWorkDonePopup.php?id=" + workDoneID + "&row=" + rowID + "&function=" + editOrDelete, null, 'dialogHeight:300px;dialogWidth:500px;dialogLeft:300;dialogTop:200');
           }
       }
     </script>
 </center>
        </div>
    </body>
</html>
